package com.eqxiu.ieditor.manager.controller;

import com.eqxiu.ieditor.commons.web.ResultData;
import com.eqxiu.ieditor.weixin.api.WeChatKeywordServiceApi;
import com.eqxiu.ieditor.weixin.dto.KeywordDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 微信公众号-自动回复-关键字管理
 */
@Controller
@RequestMapping("/m/ie/mp")
public class WeChatKeywordController {
    @Autowired
    private WeChatKeywordServiceApi weChatKeywordServiceApi;
    private static Integer MATCH_CONTENT_LENGTH = 40;
    private static Integer REPLY_CONTENT_LENGTH = 500;

    @PostMapping("/{page}")
    public String index(@PathVariable String page) {
        return "mp/" + page;
    }

    @PostMapping("/keyword/list")
    @ResponseBody
    public Map<String, Object> findByParam(KeywordDto dto, HttpServletRequest request) {
        Map<String, Object> map = weChatKeywordServiceApi.findByParam(dto);
        return map;
    }

    @PostMapping("/keyword/save")
    @ResponseBody
    public ResultData save(KeywordDto dto, HttpServletRequest request) {
        if (dto==null){
            return ResultData.result(false).setMsg("参数错误！");
        }
        if (dto.getMatchContent().length()>MATCH_CONTENT_LENGTH||dto.getReplyContent().length()>REPLY_CONTENT_LENGTH){
            return ResultData.result(false).setMsg("关键字或回复内容太长了！");
        }
        //判断关键字重复不
        if (StringUtils.isNotBlank(dto.getMatchContent()) && StringUtils.isNotBlank(dto.getReplyContent())) {
            Integer count = weChatKeywordServiceApi.checkMatchContent(dto.getMatchContent());
            if (count > 0) {
                return ResultData.result(false).setMsg("该关键字已经存在！");
            }
            weChatKeywordServiceApi.save(dto);
        }
        return ResultData.result(true);
    }

    @PostMapping("/keyword/update")
    @ResponseBody
    public ResultData update(KeywordDto dto, HttpServletRequest request) {
        if (dto==null){
            return ResultData.result(false).setMsg("参数错误！");
        }
        if (dto.getMatchContent().length()>MATCH_CONTENT_LENGTH||dto.getReplyContent().length()>REPLY_CONTENT_LENGTH){
            return ResultData.result(false).setMsg("关键字或回复内容太长了！");
        }
        //判断关键字重复不
        if (StringUtils.isNotBlank(dto.getMatchContent()) && StringUtils.isNotBlank(dto.getReplyContent())) {
            Integer count = weChatKeywordServiceApi.checkMatchContentExcludeId(dto.getMatchContent(), dto.getId());
            if (count > 0) {
                return ResultData.result(false).setMsg("该关键字已经存在！");
            }
            weChatKeywordServiceApi.update(dto);
        }
        return ResultData.result(true);
    }

    @GetMapping("/keyword/delete")
    @ResponseBody
    public ResultData delete(Integer id, HttpServletRequest request) {
        weChatKeywordServiceApi.delete(id);
        return ResultData.result(true);
    }

}
